<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-date-picker
            v-model="pickerDate"
            value-format="yyyy.MM.dd"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="仓库" v-model="formInline.store_id" style="width:160px">
            <el-option
              v-for="item in store_list"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="订单状态" v-model="formInline.type" style="width:160px">
            <el-option v-for="item in type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select placeholder="订单状态" v-model="formInline.status" filterable style="width:160px">
            <el-option v-for="item in status" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="formInline.order_no"
            placeholder="订单号查询"
            @keyup.enter.native="searchClick"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
      </el-form>

      <!-- 列表 -->
      <el-table highlight-current-row :data="list" v-loading.body="listLoading" border size="small">
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            <span class="indexColor">{{scope.$index+1+(currentPage-1)*20}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="订单状态">
          <template slot-scope="scope">
            <!-- <span v-if="scope.row.status==0">待神采审核</span>
                        <span v-if="scope.row.status==1">待平台确认</span>
                        <span v-if="scope.row.status==2">待发货</span>
                        <span v-if="scope.row.status==3">部分收货</span>
                        <span v-if="scope.row.status==4">订单完结</span>
                        <span v-if="scope.row.status==5">收货完成</span>
                        <span v-if="scope.row.status==6">订单取消</span>
                        <span v-if="scope.row.status==7">删除</span>
            <span v-if="scope.row.status==8">待收货</span>-->

            <span v-if="scope.row.status==1">待神采审核</span>
            <span v-if="scope.row.status==2">待平台确认</span>
            <span v-if="scope.row.status==3">审核关闭</span>
            <span v-if="scope.row.status==4">出库关闭</span>
            <span v-if="scope.row.status==5">待发货</span>
            <span v-if="scope.row.status==6">已发货</span>
            <span v-if="scope.row.status==7">待收货</span>
            <span v-if="scope.row.status==8">部分收货</span>
            <span v-if="scope.row.status==9">订单完成</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="本平台订单编号">
          <template slot-scope="scope">
            <el-button
              @click="get_order_info(scope.row,scope.$index)"
              type="text"
              size="small"
            >{{scope.row.order_no}}</el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="其他平台订单编号" prop="platform_order_no"></el-table-column>
        <el-table-column align="center" label="调拨出库仓" prop="store_out_name"></el-table-column>
        <el-table-column align="center" label="调拨入库仓" prop="store_in_name"></el-table-column>
        <el-table-column align="center" label="下单时间" prop="create_time"></el-table-column>
        <el-table-column align="center" label="备注">
          <template slot-scope="scope">
            <span class="indexColor">{{!scope.row.remark?'---':scope.row.remark}}</span>
          </template>
        </el-table-column>
        <!-- 列表的操作按钮 -->
        <el-table-column align="center" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.status!=1"
              @click="closeClick(scope.row.order_id,scope.row.status,scope.$index)"
              type="danger"
              size="mini"
            >关闭订单</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 弹框 -->
    <el-dialog
      :close-on-click-modal="false"
      class="dialog_table"
      title="礼品调拨单审核"
      :visible.sync="detail_dialog"
    >
      <div class="dialogInContainer" v-loading="dialogLoading">
        <div
          style="position:relative;top:-20px"
        >调拨出库仓：{{store_out_name}}&#x3000;&#x3000;调拨入库仓：{{store_in_name}}</div>
        <el-table
          highlight-current-row
          :data="infolist"
          v-loading.body="listLoading"
          border
          size="small"
        >
          <el-table-column align="center" label="序号" width="50">
            <template slot-scope="scope">
              <span class="indexColor">{{scope.$index+1}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" alt />
            </template>
          </el-table-column>
          <el-table-column align="center" label="礼品名称" prop="gift_name"></el-table-column>
          <el-table-column align="center" label="新礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
          <el-table-column align="center" label="尺寸">
            <template slot-scope="scope">{{scope.row.size?scope.row.size:'-'}}</template>
          </el-table-column>
          <el-table-column align="center" label="三级分类" prop="type_name"></el-table-column>
          <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
          <el-table-column align="center" label="订单数">
            <template slot-scope="scope">
              <el-input
                disabled
                type="number"
                onmousewheel="return false;"
                v-model="scope.row.order_num"
                placeholder="数量"
                @input="count"
              ></el-input>
            </template>
          </el-table-column>
          <!-- 列表的操作按钮 -->
          <!-- <el-table-column align="center" label="操作">
                        <template slot-scope="scope">
                            <el-switch :disabled="isDisabled" active-value="0" inactive-value="1"
                                v-model="scope.row.is_del"></el-switch>
                        </template>
          </el-table-column>-->
        </el-table>
        <!-- 自定义底部 -->
        <br />
        <div class="bottom_box">
          <el-input
            v-model="saveForm.remark"
            :maxlength="50"
            size="small"
            style="width:200px"
            placeholder="输入备注50字内"
          ></el-input>
          <el-button
            class="pull-right"
            :disabled="isDisabled"
            size="small"
            type="primary"
            @click="saveClick()"
          >确认提交</el-button>
          <p
            class="pull-right"
            style="line-height:0"
          >礼品总数量:{{totalOrder}} 礼品总金额:{{totalPrice|keepTwoNum}}</p>
        </div>
      </div>
    </el-dialog>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination
        class="pull-left"
        background
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage"
        :page-size="20"
        layout="prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import {
  getStoreList,
  getList,
  getPage,
  order_info,
  save,
  close,
  getExport,
} from "@/api/goodsflow/kua_auditing";
import { pickerOptionsF, pickerDateF, optionMaxTime } from "@/utils";
export default {
  data() {
    return {
      dialogLoading: false,
      listLoading: false,
      type_list: [
        { id: 1, name: "其他平台调入订单" },
        { id: 2, name: "神采平台调出订单" },
      ],
      status: [
        { id: -1, name: "全部" },
        { id: 1, name: "待神采审核" },
        { id: 2, name: "待其他平台确认" },
        { id: 3, name: "审核关闭" },
        { id: 4, name: "出库关闭" },
        { id: 5, name: "待发货" },
        { id: 6, name: "已发货" },
        { id: 7, name: "待收货" },
        { id: 8, name: "部分收货" },
        { id: 9, name: "订单完成" },
      ],
      formInline: {
        store_id: "",
        order_no: "",
        start_time: "",
        end_time: "",
        type: 2,
        status: -1,
      },
      pickerOptions: pickerOptionsF(),
      pickerDate: pickerDateF(),
      store_list: null,
      list: [],
      totalDataNumber: 1,
      currentPage: 1,
      detail_dialog: false,
      infolist: [],
      switchVal: true,
      totalOrder: "0",
      totalPrice: "0",
      saveForm: {
        order_id: "",
        remark: "",
        order: [],
      },
      editIndex: "",
      isDisabled: false,
      store_out_name: "",
      store_in_name: "",
    };
  },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      //当进入组件之前，执行 beforRouteEnter 路由钩子函数 ,//参数vm就是当前组件的实例。
      if (!vm.store_list) {
        vm.getstore();
      }
    });
  },
  methods: {
    getstore() {
      getStoreList({ category_id: this.$route.meta.id }).then((response) => {
        this.store_list = response;
        let data = response.filter(
          (res) => res.id == this.$store.getters.optionId
        );
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        }
      });
    },
    searchClick() {
      optionMaxTime(this.pickerDate).then(() => {
        //最大可选择时间区间
        this.formInline.start_time = this.pickerDate ? this.pickerDate[0] : "";
        this.formInline.end_time = this.pickerDate ? this.pickerDate[1] : "";
        this.fetchData();
      });
    },
    // 关闭订单
    closeClick(order_id, status, index) {
      this.editIndex = index;
      this.$confirm("此操作为关闭, 是否继续?", "提示", {
        type: "warning",
      }).then(() => {
        close({ order_id, status }).then((response) => {
          this.$message({
            type: response.code == 1 ? "success" : "error",
            message: response.message,
          });
          if (response.code == 1) {
            this.list[this.editIndex].status = 3;
          }
        });
      });
    },
    // 提交
    saveClick() {
      const that = this;
      that.saveForm.order = [];
      this.infolist.forEach(function (val, index) {
        if (val.order_num == "" || val.order_num == 0) {
          return;
        } else {
          that.saveForm.order.push({
            look_money: val.money_cost,
            gift_id: val.gift_id,
            verify_num: val.order_num,
            is_del: val.is_del,
          });
        }
      });
      if (this.saveForm.order.length == this.infolist.length) {
        this.$confirm("此操作提交, 是否继续?", "提示", {
          type: "warning",
        }).then(() => {
          save(this.saveForm).then((response) => {
            this.$message({
              type: response.code == 1 ? "success" : "error",
              message: response.message,
            });
            if (response.code == 1) {
              this.list[this.editIndex].status = 1;
              this.detail_dialog = false;
              this.searchClick();
            }
          });
        });
      } else {
        that.$message({ type: "error", message: "请将所有信息填写完整!" });
        return;
      }
    },
    // 打开订单详情
    get_order_info(row, index) {
      this.dialogLoading = true;
      this.store_out_name = row.store_out_name;
      this.store_in_name = row.store_in_name;
      this.isDisabled = row.status == 1 ? false : true;
      this.editIndex = index;
      this.saveForm.order_id = row.order_id;
      this.detail_dialog = true;
      order_info({ order_id: row.order_id, step: "?" }).then((response) => {
        this.infolist = response;
        this.count();
        this.dialogLoading = false;
      });
    },
    // 计算
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.infolist.forEach(function (val, index) {
        totalPrice += val.money_totle * 1; //累计总价
        totalOrder += val.order_num * 1;
      });
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline)
        .then((response) => {
          this.totalDataNumber = response.page * 20;
          getList(this.formInline).then((response) => {
            this.list = response;
            this.listLoading = false;
          });
        })
        .catch((res) => {
          this.listLoading = false;
        });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then((response) => {
        this.list = response;
        this.listLoading = false;
      });
    },
  },
};
</script>